﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tracy.DataModel;
using TracyGUI.View;
using TracyGUI.View.UIComponent;
using SharedLib;

namespace TracyGUI.View.EntryTabs
{
    class TabPageResources : TabPageGeneric<Resource>, IResourceTabPageView
    {
        public event EventHandler<GenericEventArgs<Resource>> ResourcesCopyingURL;
        public event EventHandler<GenericEventArgs<List<Resource>>> ResourcesRemoving;
        public event EventHandler<GenericEventArgs<List<Resource>>> ResourcesSendingToDownload;

        public TabPageResources()
        {
            //Build Context Menu
            ListViewItems.ContextMenuStrip = new ContextMenuStrip();
            ListViewItems.ContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(ContextMenuStrip_Opening);
            
            //Download
            ListViewItems.ContextMenuStrip.Items.Add(Tracy.TracyFacade.Instance.LanguageManager.GetText("MenuItem_Download")).Click += new EventHandler(DownloadMenu_Click);
            
            //Copy URL
            ListViewItems.ContextMenuStrip.Items.Add(Tracy.TracyFacade.Instance.LanguageManager.GetText("MenuItem_CopyURL")).Click += new EventHandler(CopyURLMenu_Click);
                        
            //Remove
            ListViewItems.ContextMenuStrip.Items.Add(Tracy.TracyFacade.Instance.LanguageManager.GetText("MenuItem_Remove")).Click += new EventHandler(RemoveMenu_Click);
        }

        void CopyURLMenu_Click(object sender, EventArgs e)
        {
            List<Resource> resources = GetSelectedDataItems();
            if (resources.Count > 0)
            {
                ResourcesCopyingURL.InvokeEvent(this, new GenericEventArgs<Resource>(resources[0]));
            }
        }

        void DownloadMenu_Click(object sender, EventArgs e)
        {
            List<Resource> resources = GetSelectedDataItems();
            if (resources.Count > 0)
            {
                ResourcesSendingToDownload.InvokeEvent(this, new GenericEventArgs<List<Resource>>(resources));
            }
        }

        void RemoveMenu_Click(object sender, EventArgs e)
        {
            List<Resource> resources = GetSelectedDataItems();
            if (resources.Count > 0)
            {
                ResourcesRemoving.InvokeEvent(this, new GenericEventArgs<List<Resource>>(resources));
            }
        }

        void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (GetSelectedDataItems().Count == 0) e.Cancel = true;
        }

        protected override void InitColumns()
        {
            //TODO: load from settings
            _listViewItems.AddColumn("Title", Tracy.TracyFacade.Instance.LanguageManager.GetText("ColumnName_Title"));
            _listViewItems.AddColumn("Source", Tracy.TracyFacade.Instance.LanguageManager.GetText("ColumnName_Source"));
            _listViewItems.AddColumn("FanSubGroup", Tracy.TracyFacade.Instance.LanguageManager.GetText("ColumnName_FanSubGroup"));
            _listViewItems.AddColumn("Format", Tracy.TracyFacade.Instance.LanguageManager.GetText("ColumnName_Format"));
            _listViewItems.AddColumn("Resolution", Tracy.TracyFacade.Instance.LanguageManager.GetText("ColumnName_Resolution"));
            _listViewItems.AddColumn("Status", Tracy.TracyFacade.Instance.LanguageManager.GetText("ColumnName_Status"));
            _listViewItems.AddColumn("Created", Tracy.TracyFacade.Instance.LanguageManager.GetText("ColumnName_Created"), ListViewColumnSorter.COLUMN_TYPE_DATE);
            _listViewItems.Columns["Title"].Width = 500;
        }

        protected override ListViewItem CreateListViewItem(Resource dataItem)
        {
            ListViewItem lvItem = new ListViewItem();
            lvItem.Name = dataItem.ID;
            lvItem.Tag = dataItem;
            lvItem.Text = dataItem.Title;

            EnhancedListView.AddSubListViewItem(lvItem, "Source", (dataItem.FeedSource == null) ? "" : dataItem.FeedSource.Name);
            EnhancedListView.AddSubListViewItem(lvItem, "FanSubGroup", (dataItem.FanSubGroups == null) ? "" : FanSubGroup.GetCombinationName(dataItem.FanSubGroups));
            EnhancedListView.AddSubListViewItem(lvItem, "Format", (dataItem.Format == null) ? "" : dataItem.Format.Name);
            EnhancedListView.AddSubListViewItem(lvItem, "Resolution", (dataItem.Resolution == null) ? "" : dataItem.Resolution.Name);
            EnhancedListView.AddSubListViewItem(lvItem, "Status", Tracy.TracyFacade.Instance.LanguageManager.GetText("ResourceStatus_" + dataItem.Status));
            EnhancedListView.AddSubListViewItem(lvItem, "Created", dataItem.Created);
            return lvItem;
        }

        protected override void UpdateListViewItem(ListViewItem lvItem, Resource dataItem)
        {
            lvItem.SubItems["Status"].Text = Tracy.TracyFacade.Instance.LanguageManager.GetText("ResourceStatus_" + dataItem.Status);
        }

        #region ITabPageView Members


        public TabPage TabPage
        {
            get { return this; }
        }

        #endregion
    }
}
